/**
 * Created by KhoaNam on 7/9/2014.
 */

var MessageBox = cc.Class.extend( {
    ctor: function(){
        //this._super();
        this.startLogoSprite = null;
        this.bkgSprite = null;
        this.parent = null;
        this.textSprite = null;
        this.isShow = false;
        this.button = null;
        this.typeButton = INFO.MESSAGEBOX.BUTTON_READY;
    },
    init: function(){

    },

    getStartLogoSprite: function(){
        if(this.startLogoSprite == null){
            this.startLogoSprite = cc.Sprite.create(MESSAGEBOX.START_LOGO);
        }
        return this.startLogoSprite;
    },

    getBackgroundSprite: function(){
        if(this.bkgSprite == null){
            this.bkgSprite = cc.Sprite.create(MESSAGEBOX.BACKGROUND);
        }
        return this.bkgSprite;
    },

    getTextSprite: function(){

        if(this.textSprite == null){
            this.textSprite = cc.LabelTTF.create("",
                MESSAGEBOX.FONT, MESSAGEBOX.FONT_SIZE, cc.size(this.getBackgroundSprite().getContentSize().width , this.getBackgroundSprite().getContentSize().height), cc.TEXT_ALIGNMENT_CENTER, cc.VERTICAL_TEXT_ALIGNMENT_CENTER);
        }
        return this.textSprite;
    },

    setText : function(text){
        this.getTextSprite().setString(text);
    },

    _getStartLogoPosition: function(){
        var position = this.getBackgroundSprite().getPosition();
        position.y += this.getBackgroundSprite().getContentSize().height/2;
        return position;
    },

    getButtonSprite: function(){
        if(this.button == null){
            var data = INFO.BUTTON.READY;
            switch (this.typeButton){
                case INFO.MESSAGEBOX.BUTTON_READY:
                    data = INFO.BUTTON.READY;
                    break;
                case INFO.MESSAGEBOX.BUTTON_START:
                    data = INFO.BUTTON.START;
                    break;
                case INFO.MESSAGEBOX.BUTTON_QUIT:

                    break;
            }

            this.button = new MyButton(data);
        }
        return this.button;
    },

    addToLayer: function(layer){
        this.parent = layer;
    },

    show: function (x, y) {
        if(this.parent != null) {
            this.isShow = true;
            this.parent.addChild(this.getBackgroundSprite());
            this.parent.addChild(this.getStartLogoSprite());
            this.parent.addChild(this.getTextSprite());
            this.getTextSprite().setPosition(x, y);
            this.getBackgroundSprite().setPosition(x, y);
            var pos = this._getStartLogoPosition();
            this.getStartLogoSprite().setPosition(pos.x, pos.y);
        }
    },

    hide: function() {
        if (this.parent != null && this.isShow) {
            this.isShow = false;
            this.parent.removeChild(this.getBackgroundSprite());
            this.parent.removeChild(this.getStartLogoSprite());
            this.parent.removeChild(this.getTextSprite());

        }
    },

    showButton: function(typeButton){
        if(typeButton != undefined) {
            this.typeButton = typeButton;
        }

        if(this.parent != null){
            var pos = this.getBackgroundSprite().getPosition();
            pos.y = pos.y - this.getBackgroundSprite().getContentSize().height/2 + INFO.MESSAGEBOX.PADDING +  this.getButtonSprite().getContentSize().height/2;
            this.getButtonSprite().setPosition(pos.x, pos.y);
            this.parent.addChildHasEvent(this.getButtonSprite(), 10);
        }
    },

    hideButton: function(){
        if(this.parent != null){
            if(this.button != null){
                this.parent.removeChildHasEvent(this.getButtonSprite());
                delete this.button;
                this.button = null;
            }
        }
    }
});

